﻿using IM.Easy.AuthorizationServices.Dtos;
using IM.Easy.AuthorizationServices.IServices;
using IM.Easy.Core.ServiceLifetime;
using IM.Easy.Core.Services;
using IM.Easy.CoreServices.IServices;
using IM.Easy.Entity.System;
using IM.Easy.Infrastructure;
using IM.Easy.Infrastructure.Constants;
using IM.Easy.SystemServices.IServices;
using IM.Easy.SystemServices.Services;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection.Metadata;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace IM.Easy.Api.AccountManage
{
    /// <summary>
    /// 权限服务
    /// </summary>
    public class PermissionService : IPermissionService
    {
        private readonly IUserRoleService userRoleService;
        private readonly IRoleMenuService roleMenuService;
        private readonly IParamsService paramsService;
        public PermissionService(IUserRoleService userRoleService, IRoleMenuService roleMenuService, IParamsService paramsService)
        {
            this.userRoleService = userRoleService;
            this.roleMenuService = roleMenuService;
            this.paramsService = paramsService;
        }

        /// <summary>
        /// 绑定账户角色
        /// </summary>
        public void BindAccountRole(AccountInfo account)
        {
            if (account.IsSuperAdmin)
            {
                account.Roles = [GlobalConstant.SuperAdmin];
                return;
            }
            else if (account.IsLocalAccount)
            {
                var roles = GetUserRoles(account.Id);
                account.Roles = roles.Select(s => s.Code).ToList();
            }
            else
            {
                var scope = paramsService.GetValue(ParamsConstant.ExternalScope);
                account.Roles = [scope];
            }
        }

        /// <summary>
        /// 绑定账户权限
        /// </summary>
        public void BindAccountPermission(AccountInfo account)
        {
            var perms = new List<string>();
            if (account.IsSuperAdmin)
            {
                perms = [GlobalConstant.SuperPermission];
            }
            else if (account.IsLocalAccount)
            {
                perms = GetMenuPerms(account.Id);
            }
            else
            {
                var scope = paramsService.GetValue(ParamsConstant.ExternalScope);
                perms = GetMenuPerms(scope);
            }
            account.Permissions = perms;
        }

        /// <summary>
        /// 绑定账户功能模块
        /// </summary>
        public void BindFeature(AccountInfo account)
        {
            var features = account.IsSuperAdmin ? [GlobalConstant.SuperFeature] : GetFeatures(account.Id);
            account.Features = features;
        }

        /// <summary>
        /// 获取用户所有角色
        /// </summary>
        /// <param name="uid">userid</param>
        public List<SysRole> GetUserRoles(long uid)
        {
            var roles = userRoleService.GetUserRoles(uid);
            return roles;
        }

        /// <summary>
        /// 获取菜单权限字符串
        /// </summary>
        /// <returns></returns>
        public List<string> GetMenuPerms(long uid)
        {
            var roles = GetUserRoles(uid);
            var rids = roles.Select(s => s.Id).ToArray();
            var menus = roleMenuService.GetMenus(rids);
            var perms = menus.Where(s => s.Perm?.Count > 0).SelectMany(s => s.Perm).ToList();
            return perms;
        }

        /// <summary>
        /// 获取菜单权限字符串
        /// </summary>
        /// <returns></returns>
        public List<string> GetMenuPerms(string roleKey)
        {
            var menus = roleMenuService.GetMenus(roleKey);
            var perms = menus.Where(s => s.Perm?.Count > 0).SelectMany(s => s.Perm).ToList();
            return perms;
        }

        public List<string> GetFeatures(long uid)
        {
            var roles = GetUserRoles(uid);
            var rids = roles.Select(s => s.Id).ToArray();
            var menus = roleMenuService.GetMenus(rids);
            var features = menus.Where(s => !string.IsNullOrEmpty(s.FuncKey)).Select(s => s.FuncKey).Distinct().ToList();
            return features;
        }

        public List<string> GetFeatures(string roleKey)
        {
            var menus = roleMenuService.GetMenus(roleKey);
            var features = menus.Where(s => !string.IsNullOrEmpty(s.FuncKey)).Select(s => s.FuncKey).Distinct().ToList();
            return features;
        }
    }
}
